home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / BOXES / THSTCBO2 / HSTCBO.PAS < prev    next >
Pascal/Delphi Source File  |  1996-05-26  |  5KB  |  177 lines

  1. unit Hstcbo;
  2.  
  3. {******************************************************************************
  4.  
  5.   THistoryComboBox:
  6.   FREEWARE !
  7.   Version 1.5, May 96
  8.   Author: Christian Holzner, Austria
  9.           Internet: cholzner@ping.at
  10.  
  11. ********************************************************************************
  12.  
  13.   Reads/Writes Items of a TComboBox to/from Registry.
  14.   The reading and writing has to be initiated manually (methods: ReadRegistry,
  15.   WriteRegistry). It could also done in Create/Destroy but then I would loose
  16.   flexibility.
  17.  
  18.   If you find any bugs or enhance this component I would like to get an info.
  19.  
  20. ********************************************************************************
  21.  
  22.   Added properties to TComboBox
  23.  
  24.     Key:               Key in Registry
  25.                        If left blank then no Registry-access is made
  26.                        (= it behaves like a normal ComboBox)
  27.     FMaxHistoryLength: Maxmimum allowed length of the history-items
  28.  
  29.  
  30.   Added methods to TComboBox
  31.  
  32.     ReadRegistry:      Reads items from Registry
  33.     WriteRegistry:     Writes items to Registry
  34.     AddToList:         Adding an item to the Historylistbox
  35.  
  36. *******************************************************************************
  37.  
  38.   History:
  39.     1.0, Jan. 96: Uses Ini-file for storage.
  40.     1.5, May 96 : Uses Registry for storage
  41.                   Replaced 'Exit' with 'Break' which caused memory leak. (My
  42.                   Clipper history affected me with the 'Exit' statement ;-)
  43.                   New method 'AddToList' by Jeff Goldberg (jeffg@haven.ios.com)
  44.  
  45. *******************************************************************************}
  46.  
  47. interface
  48.  
  49. uses
  50.   SysUtils, Windows, Classes, StdCtrls, Forms, Registry;
  51.  
  52. type
  53.   THistoryComboBox = class(TComboBox)
  54.   private
  55.     FKey: String;
  56.     FMaxHistoryLength: Integer;
  57.   protected
  58.     { Protected-Deklarationen }
  59.   public
  60.     constructor Create(AOwner: TComponent); Override;
  61.     procedure ReadRegistry;
  62.     procedure WriteRegistry;
  63.     procedure AddToList(NewItem: String);
  64.   published
  65.     property Key: String
  66.              read FKey write FKey;
  67.     property MaxHistoryLength: Integer
  68.              read FMaxHistoryLength write FMaxHistoryLength
  69.              default 7;
  70.   end;
  71.  
  72. procedure Register;
  73.  
  74. implementation
  75.  
  76.  
  77. constructor THistoryComboBox.Create(AOwner: TComponent);
  78. begin
  79.   inherited Create(AOwner);
  80.   FMaxHistoryLength := 7;
  81. end;
  82.  
  83.  
  84. procedure THistoryComboBox.ReadRegistry;
  85. { Reads items from Registry }
  86. var
  87.   Registry: TRegistry;
  88.   i : Integer;
  89.   FileName, Value: String;
  90. begin
  91.   if FKey <> '' then begin
  92.  
  93.     { Default Rootkey is 'HKey_Current_User\Software' }
  94.     Registry := TRegistry.Create;
  95.     try
  96.       Clear;
  97.       { Read maximum allowed entries from Registry }
  98.       Registry.RootKey := HKey_Current_User;
  99.       if Registry.OpenKey('\Software\'+FKey,False) then begin
  100.         for i:= 0 to FMaxHistoryLength-1 do begin
  101.            Value := Registry.ReadString('H'+IntToStr(i));
  102.            If Value <> ''
  103.              then Items.Add(Value)
  104.              else break;
  105.         end;
  106.       end;
  107.     finally
  108.       Registry.Free;
  109.     end;
  110.  
  111.   end;
  112. end;
  113.  
  114.  
  115. procedure THistoryComboBox.WriteRegistry;
  116. { Writes items to registry }
  117. var
  118.   Registry: TRegistry;
  119.   FileName: String;
  120.   i : Integer;
  121. begin
  122.   if FKey <> '' then begin
  123.  
  124.   { Default Rootkey is 'HKey_Current_User\Software' }
  125.     Registry := TRegistry.Create;
  126.     try
  127.       { Read maximum allowed entries from Registry }
  128.       Registry.RootKey := HKey_Current_User;
  129.       if Registry.OpenKey('\Software\'+FKey,False) then begin
  130.         try
  131.           for i:= 0 to FMaxHistoryLength-1 do
  132.              Registry.ReadString('H'+IntToStr(i));
  133.         except
  134.           on ERegistryException do;
  135.         end;
  136.       end;
  137.  
  138.       { Erase existing entries to get rid of more then the max. allowed entries }
  139.       if Registry.OpenKey('\Software',False) then
  140.         Registry.DeleteKey('FKey');
  141.       { Add to Historylistbox }
  142.       AddToList(Text);
  143.       { Write to Registry }
  144.       if Registry.OpenKey('\Software\'+FKey,True) then
  145.       for i := 0 to Items.Count-1 do
  146.          Registry.WriteString('H'+IntToStr(i),Items[i]);
  147.     finally
  148.       Registry.Free;
  149.     end;
  150.  
  151.   end;
  152. end;
  153.  
  154.  
  155. procedure THistoryComboBox.AddToList(NewItem: String);
  156. { Add a string to the Historylistbox }
  157. var
  158.    i: integer;
  159. begin
  160.    if NewItem <> '' then begin
  161.      { Insert in first position }
  162.      Items.Insert(0,NewItem);
  163.      { Check maximum numer of entries and delete any duplicate }
  164.      for i := 1 to Items.Count-1 do
  165.        if (Items[i] = NewItem) or (i > FMaxHistoryLength-1) then
  166.          Items.Delete(i);
  167.    end;
  168. end;
  169.  
  170.  
  171. procedure Register;
  172. begin
  173.   RegisterComponents('Standard',[THistoryComboBox]);
  174. end;
  175.  
  176. end.
  177.